package com.xca.ninedemo.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xca.ninedemo.R;
import com.xca.ninedemo.entity.Weather;
import com.xca.ninedemo.utils.TimeUtils;

import java.text.ParseException;
import java.util.List;

public class WeatherWeekAdapter extends RecyclerView.Adapter<WeatherWeekAdapter.ViewHolder>{
    private List<Weather.Result.Future> mList ;
    private Context mContext ;
    private Activity activity ;
    public WeatherWeekAdapter(List<Weather.Result.Future> list , Context context , Activity activity)
    {
        this.mList = list ;
        this.mContext = context ;
        this.activity = activity ;
    }

    public void updateDate(List<Weather.Result.Future> list)
    {
        mList.clear();
        this.mList = list ;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(mContext , R.layout.item_weather_week , null);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Weather.Result.Future future = mList.get(position);
        try {
            holder.tv1.setText(TimeUtils.extractMonthDay(future.getDate()));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        holder.tv2.setText(future.getWeather());
        holder.tv3.setText(future.getTemperature());
        holder.tv4.setText(future.getDirect());
        holder.tv5.setText(future.getWid().getDay() + "/" + future.getWid().getNight());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv1 , tv2 , tv3 , tv4 , tv5 ;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv1 = itemView.findViewById(R.id.wk_time);
            tv2 = itemView.findViewById(R.id.wk_weather);
            tv3 = itemView.findViewById(R.id.wk_temperature);
            tv4 = itemView.findViewById(R.id.wk_wind);
            tv5 = itemView.findViewById(R.id.wk_wind_power);
        }
    }
}
